package com.android.antelopeapp.view.CNArticle;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.allthelucky.common.view.ImageIndicatorView;
import com.android.antelopeapp.R;
import com.android.antelopeapp.bean.CNContent;
import com.android.antelopeapp.external.paginglistview.PagingBaseAdapter;
import com.android.antelopeapp.network.NetworkManager;
import com.android.volley.toolbox.ImageLoader;

import java.util.ArrayList;
import java.util.List;
/**
 * @author PHK
 * 精选页面用于显示推荐内容的listview
 * CN表示Choiceness
 */
public abstract class CNCAdapter extends PagingBaseAdapter<CNContent> implements OnClickImgBtnListener,OnClickImgBgListener{
    private String TAG = "CNCAdapter";
	private Context _context;
	private ImageIndicatorView _imgIntorView;
	private List<ViewHolder> viewHolderList = new ArrayList<ViewHolder>();
	private int viewResource = R.layout.view_cn_content_item;
	
	/**
	 * 用于普通的文章列表
	 * */
	public CNCAdapter(Context context) {
		super();
		this._context = context;
	}
	/**
	 * 设置默认的view所对应的xml资源
	 * */
    public void setViewResource(int res){
    	viewResource = res;
    }


    public CNCAdapter(ImageIndicatorView iiv, Context context) {
        super();
        this._imgIntorView = iiv;
        this._context = context;
    }
	
	public CNCAdapter(List<CNContent> list,ImageIndicatorView iiv, Context context) {
		super();
		this._imgIntorView = iiv;
		this._context = context;
		this.items = list;
	}

	//设置Holder的内容，并绑定点击事件
	private void fillHolder(final int position,ViewHolder holder){
        CNContent cn = items.get(position);
		holder.desptText.setText(cn.get_description());
		holder.likeCnt.setText(cn.get_likeCount()+"");
        Log.v(TAG,"content imgurl :"+cn.get_indicateImgURL());
        if(cn.get_indicateImgURL()==null){
            //todo 未获取到图片url地址，需要进行处理
            holder.imgBg.setImageResource(R.drawable.indicate_img_1);
        }else{
            //发送网络请求获取
            ImageLoader imageLoader = NetworkManager.getInstance(_context).getImageLoader();
            imageLoader.get(NetworkManager.getResourceAbsPath(cn.get_indicateImgURL()), ImageLoader.getImageListener(holder.imgBg,
                    R.drawable.indicate_img_2, R.drawable.indicate_img_1));
        }
		holder.imgBg.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				OnClickImgBg(position);
			}
		});

		if(holder.imgBtnMore != null){
			holder.imgBtnMore.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					OnClickImgBtn(position);
				}
			});
		}
	}

    public CNContent getSpeItem(int pos){
        return items.get(pos);
    }

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		//观察convertView随ListView滚动情况
		Log.v("CNCAdapter","getView "+position+" "+convertView);
		
		ViewHolder holder;
		if(convertView == null || convertView == _imgIntorView){
			convertView = LayoutInflater.from(_context).inflate(viewResource, null);
			//convertView = View.inflate(_context, viewResource, null);
			holder = new ViewHolder();
			holder.desptText = (TextView) convertView.findViewById(R.id.item_text);
			holder.likeCnt = (TextView) convertView.findViewById(R.id.item_like);
			holder.imgBg = (ImageView) convertView.findViewById(R.id.item_bg);
			holder.imgBtnMore = (ImageButton) convertView.findViewById(R.id.item_btn_more);
			convertView.setTag(holder);
			viewHolderList.add(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();//取出viewholder对象
		}
		fillHolder(position,holder);
		return convertView;
	}
	public final class ViewHolder{
    	public ImageButton imgBtnMore;
    	public ImageView imgBg;
    	public TextView desptText;
    	public TextView likeCnt;
    }
 }
interface OnClickImgBtnListener{
	public void OnClickImgBtn(int position);
}

interface OnClickImgBgListener{
	public void OnClickImgBg(int position);
}
    
